package com.swm.util;

import com.alibaba.fastjson2.JSONObject;
import com.swm.mapper.StockDayDataMapper;
import com.swm.mapper.StockMapper;
import com.swm.pojo.Stock;
import com.swm.pojo.Stock_Day_Data;
import org.apache.ibatis.session.SqlSession;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import static com.swm.util.getStockDayData4.days;

/**
 * @author 师文明
 * @data 2024/12/23 下午3:11
 **/
public class getStockDayData461 {

    public static int allup;
    public static double allupnum;
    public static double sum;
    public static int num0;
    public static int numu;
    public static int numd;
    public static DecimalFormat df = new DecimalFormat("#0.000");
    public static int ban = 0;
    static SqlSession sqlSession = SqlSessionUtil.openSession();
    static StockMapper mapper = sqlSession.getMapper(StockMapper.class);
    static StockDayDataMapper stockDayDataMapper = sqlSession.getMapper(StockDayDataMapper.class);

    private static ArrayList<String> dateList = new ArrayList<String>();
    private static int count=0;
    public static List<Stock> getStcokList() {
        List<Stock> stocks = mapper.selectAllStock();
        return stocks;
    }

    public static void main(String[] args) throws Exception {
        List<Stock_Day_Data> stockDayData = stockDayDataMapper.selectAllStockDayData();
        System.out.println(stockDayData.size());
        if(stockDayData.size()>0){
            ExecutorService executorService = Executors.newFixedThreadPool(10);
            //获取所有股票列表
            List<Stock> stcokList = getStcokList();
            for (Stock stock : stcokList) {
                executorService.execute(() -> {
                    try {
                        getData(stock,stockDayData);
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                });
            }
            Thread.sleep(150000);
            double v = 0;
            if (num0 != 0 && sum != 0) {
                v = sum / num0;
            }

            System.out.println(("一天后涨的次数：" + numu + "  跌的次数：" + numd + "  上涨概率： " + df.format((double) numu * 100 / num0)) + "%  平均涨幅：" + df.format(v));
            System.out.println("板数：" + ban);

            System.out.println("中股天数："+dateList.size());
        }

      /*dateList.forEach(date->{
           System.out.println(date);
       });*/

        // executorService.shutdown();
    }

    public static void getData(Stock stock,List<Stock_Day_Data> stockDayData1) throws Exception {
        count++;
       // System.out.println(count);
       // List<Stock_Day_Data> stockDayData = stockDayDataMapper.selectStockDayData(stock.getId());
        List<Stock_Day_Data> stockDayData = new ArrayList<Stock_Day_Data>();
        stockDayData1.forEach(stockDayData2 -> {
            if(stockDayData2.getCode().equals(stock.getCode())){
                stockDayData.add(stockDayData2);
            }
        });
        if (stockDayData.size() >  200) {
            for (int i = 20; i < 120; i++) {
                //昨日涨幅
                double v= stockDayData.get(i).getZdf();
                //前日涨幅
                double v00= stockDayData.get(i+1).getZdf();
                //大前日涨幅
                double v000= stockDayData.get(i+2).getZdf();


                JSONObject lines = getLines(stockDayData, i);
                double f1 = Double.parseDouble(lines.get("f1").toString());
                double te1 = Double.parseDouble(lines.get("te1").toString());
                double tw1 = Double.parseDouble(lines.get("tw1").toString());
                double th1 = Double.parseDouble(lines.get("th1").toString());
                double six1 = Double.parseDouble(lines.get("six1").toString());

                //开盘价
                double k= stockDayData.get(i).getK();

                //收盘价
                double s= stockDayData.get(i).getS();

                //最高价
                double g= stockDayData.get(i).getG();
                //最低价
                double d= stockDayData.get(i).getD();

                double k1= stockDayData.get(i+1).getK();

                //收盘价
                double s1= stockDayData.get(i+1).getS();

                //最高价
                double g1= stockDayData.get(i+1).getG();
                //最低价
                double d1= stockDayData.get(i+1).getD();

                //当天振幅
                double v6 = (g - d) / k;
                double v7 = (s - k) / k;


                boolean bf = d < f1 && s > f1;
                boolean bte = d < te1 && s > te1;
                boolean btw = d < tw1 && s > tw1;
                boolean bth = d < th1 && s > th1;
                boolean bsix = d < six1 && s > six1;
                String word = "";
                int flag = 0;
                if (bf) {
                    word = word.concat("上穿五日线");
                    flag++;
                }
                if (bte) {
                    word = word.concat("上穿10日线");
                    flag++;
                }
                if (btw) {
                    word = word.concat("上穿20日线");
                    flag++;
                }
                if (bth) {
                    word = word.concat("上穿30日线");
                    flag++;
                }
                if (bsix) {
                    word = word.concat("上穿60日线");
                    flag++;
                }


                boolean price = getPrice(stockDayData, i);
                //收盘与最高差距
                double v0000 = (g - s) * 100 / k;
                //boolean price120 = getPrice12010(klines, i);

                //今日成交额
                double m0 = stockDayData.get(i).getMoney();
                //昨日成交额
                double m1= stockDayData.get(i+1).getMoney();
                double m4 = m0 / m1;

                if (flag >1&&flag<4/*&&v7 > 0.03 && v < 9.8 && v > 0 && v0000 < 0.2 && price*/) {
                double v1 = (s1 - s) / s;
                //if(s>k&&s<k1&&(s>s1||v1<0.01)&&s1==d&&g1>s1&&s1<k1&&k<k1){

                    //次日涨幅
                    double v11= stockDayData.get(i-1).getZdf();

                    allupnum += v11;
                    allup++;
                    if (v11 > 0 || v11 == 0) {
                        numu++;
                    } else {
                        numd++;
                    }
                    sum += v11;
                    num0++;

                    if (!dateList.contains(stockDayData.get(i).getGetdate())) {
                        dateList.add(stockDayData.get(i).getGetdate());
                    }
                    if (v11 > 9.8) {
                        ban++;
                    }

                    System.out.println(stockDayData.get(i).getName() + " : " + stockDayData.get(i).getGetdate() + "  当日涨幅" + v + "   次日涨幅" + v11);
                }
            }
            }
    }

    //获取所有均线
    public static JSONObject getLines(List<Stock_Day_Data> stockDayData, int startDay) {
        //日线
        double totalsix1 = 0.0;
        double totalth1 = 0.0;
        double totaltw1 = 0.0;
        double totalte1 = 0.0;
        double totalf1 = 0.0;
        for (int j = 0; j < 60; j++) {
            double price = stockDayData.get(startDay + j).getS();

            if (j < 5) {
                totalf1 += price;
            }
            if (j < 10) {
                totalte1 += price;
            }
            if (j < 20) {
                totaltw1 += price;

            }
            if (j < 30) {
                totalth1 += price;

            }
            totalsix1 += price;

        }
        double six1 = totalsix1 / 60;
        double th1 = totalth1 / 30;
        double tw1 = totaltw1 / 20;
        double te1 = totalte1 / 10;
        double f1 = totalf1 / 5;
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("f1", f1);
        jsonObject.put("te1", te1);
        jsonObject.put("tw1", tw1);
        jsonObject.put("th1", th1);
        jsonObject.put("six1", six1);
        return jsonObject;

    }

    //近120天 价格处于低位
    public static boolean getPrice(List<Stock_Day_Data> stockDayData, int startDay) {
        //最近120天内收盘价集合
        List<Double> prices = new ArrayList<>();
        double s1 = stockDayData.get(startDay + 1).getS();
        double s2 = stockDayData.get(startDay).getS();
        for (int j = 0; j < 10; j++) {
            //收盘价
            double p2 = stockDayData.get(startDay + j).getS();
            prices.add(p2);
        }
        // 使用List的sort()方法和Lambda表达式进行排序
        prices.sort(Double::compare);
        return s2 < prices.get(2)/*s1==prices.get(0)*/;
    }
}

